package com.kylexy.closes.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.kylexy.closes.R;
import com.kylexy.closes.app.AppContext;
import com.kylexy.closes.app.UIHelper;
import com.kylexy.closes.bean.CloseInfo;
import com.kylexy.closes.tools.MenuUtil;
import com.kylexy.closes.ui.adapter.CloseInfoAdapter;
import com.lidroid.xutils.exception.DbException;

public class ActivityCloseInfoList extends Activity {

	Context _context;

	ListView _listView;
	List<CloseInfo> _listData = new ArrayList<CloseInfo>();

	CloseInfoAdapter _adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_closeinfo_list);
		_context = this;

		initTitle();
		initView();

		// getData();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		if (_adapter != null) {
			getData();
		}

		super.onResume();
	}

	private void initTitle() {
		View _view = findViewById(R.id.top_bar);

		MenuUtil.setMenuBack(this, _view);
		MenuUtil.setMenuTitle(_view, "商品管理");

		MenuUtil.setMenuMore(this, _view, true, "编辑", new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void initView() {
		// 新增按钮
		findViewById(R.id.closeInfo_new).setOnClickListener(_newListener);

		_listView = (ListView) findViewById(R.id.closeInfo_list);
		_adapter = new CloseInfoAdapter(_context);
		_listView.setAdapter(_adapter);
		_listView.setOnItemClickListener(_itemListner);

		UIHelper.setList(this, _listView);

	}

	private OnClickListener _newListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			UIHelper.StartCloseInfoEdit(_context, null);
		}
	};

	private OnItemClickListener _itemListner = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			CloseInfo data = _listData.get(arg2);
			UIHelper.StartCloseInfoEdit(_context, data);
		}

	};

	// 获取数据
	private void getData() {

		// _listData.add(new CloseInfo("12132312", "商品1"));
		// _listData.add(new CloseInfo("23124214", "商品2"));

		try {
			List<CloseInfo> findDatas = AppContext.getInstance().mDb
					.findAll(CloseInfo.class);

			if (findDatas == null) {
				findDatas = new ArrayList<CloseInfo>();
			}

			_listData.clear();
			_listData.addAll(findDatas);
		} catch (DbException e) {
			// TODO Auto-generated catch block
			UIHelper.ToastMessage(_context, "查询商品列表数据失败!" + e.getMessage());
			e.printStackTrace();
		}

		_adapter.setDatas(_listData);
		_adapter.notifyDataSetChanged();

	}
}
